<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/6 15:07
 * Desc: 产品营销策略处理
 */
class Model_Api_V2_Marketing_Strategy
{

    /**
     * 支持的营销应用
     */
    const MARKETING_APPLICATION = array(
        'coupon', 'envelope', 'together', 'seckill', 'card', 'member_Discount', 'integral'
    );

    /**
     * @var null
     */
    private static $instance = null;

    /**
     * @var int 产品类型id
     */
    private $type_id;

    /**
     * 支持的营销应用
     * @var array
     */
    private $support_application = array();

    public function __construct($type_id, $params = array())
    {
        $this->type_id = $type_id;

        $marketing_application = self::MARKETING_APPLICATION;
        foreach ($marketing_application as $item) {
            $class_name = 'Model_Api_V2_Marketing_Strategy_' . ucfirst($item);
            $class      = call_user_func(array($class_name, 'get_instance'), $this->type_id, $params);
            if (!empty($class)) {
                $this->support_application[$item] = $class;
            }
        }
    }

    public static function get_instance($type_id, $params = array()): Model_Api_V2_Marketing_Strategy
    {
        if (empty(self::$instance)) {
            self::$instance = new Model_Api_V2_Marketing_Strategy($type_id, $params);
        }
        return self::$instance;
    }

    /**
     * 返回支持的营销应用
     * @param $product_id
     * @return array
     */
    public function get_support_application($product_id)
    {
        $strategies = array();
        foreach ($this->support_application as $value) {
            $strategy = $value->is_support($product_id);
            if ($strategy) {
                if (is_array($strategy[0])) {
                    $strategies = array_merge($strategies, $strategy);
                } else {
                    $strategies[] = $strategy;
                }
            }
        }
        return $strategies;
    }
}